import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl

df = pd.read_csv('D:\qiliuer\考研数据分析\数据分析\数据统计.csv', encoding='GBK')
data = df.sort_values('招生总人数', ascending=False)
students = list(data['招生总人数'])[:15]    # 招生总人数取前15
province = list(data['省/市'])[:15]  # 省/市15
students_sum = pd.DataFrame(data, columns=['招生总人数']).sum()['招生总人数']  # 总人数求和
province.append('其他地区')
# print(students, province)
nums = [students[0], students[1], students[2], students[3], students[4], students[5],
        students[6], students[7], students[8], students[9], students[10], students[11], students[12], students[13], students[14], students_sum - sum(students)]
# print(nums)
colors = ['#0000CD', '#FF0000', '#FFD700', '#7FFF00',
          '#FF1493', '#9400D3', '#FF8C00', '#87CEFA']

# 设置中文显示
mpl.rcParams['font.family'] = 'SimHei'
# 设置大小  像素
plt.figure(figsize=(9, 6), dpi=100)
plt.axes(aspect='equal')   # 保证饼图是个正圆
explodes = [0, 0, 0, 0, 0, 0, 0, 0, 0.01,
            0.03, 0.05, 0.07, 0.09, 0.11, 0.13, 0.15]
plt.pie(nums, pctdistance=0.75, shadow=False,
        colors=colors, autopct='%.2f%%', explode=explodes,
        startangle=15, labeldistance=1.1,
        )
# 设置图例   调节图例位置
plt.legend(province, bbox_to_anchor=(1.0, 1.0))
plt.title('各省/市招生人数分布', fontsize=15)
plt.show()
